<?php

namespace app\api\Service;

use app\common\library\Auth;
use think\Db;
class InvoiceService
{
    // 添加抬头
    public static function add($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        $class = isset($data['class']) && $data['class'] ? $data['class'] : 1;
        $title = $data['title'];
        $mobile = $data['mobile'];
        $email = $data['email'];
        if(!$title) return ['code'=>0,'msg'=>"发票抬头不为空", 'data'=>''];
        if(!$mobile) return ['code'=>0,'msg'=>"联系电话不为空", 'data'=>''];
        if(!$email) return ['code'=>0,'msg'=>"联系邮箱不为空", 'data'=>''];
        $params['user_id'] = $user_id;
        $params['class'] = $class;
        $params['title'] = $title;
        $params['mobile'] = $mobile;
        $params['email'] = $email;

        $is_default = isset($data['is_default']) && $data['is_default'] ? $data['is_default'] : 0;
        $params['is_default'] = $is_default;

        try{
            if($is_default){
                \app\admin\model\Invoice::where(['user_id' => $user_id])->update(['is_default'=>0]);
            }

            $res = \app\admin\model\Invoice::create($params);

            Db::commit();
            if($res){
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }

    }

    // 编辑抬头
    public static function edit($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        $id = $data['id'];
        $class = isset($data['class']) && $data['class'] ? $data['class'] : 1;
        $title = $data['title'];
        $mobile = $data['mobile'];
        $email = $data['email'];
        if(!$id) return ['code'=>0,'msg'=>"无效参数", 'data'=>''];
        if(!$title) return ['code'=>0,'msg'=>"发票抬头不为空", 'data'=>''];
        if(!$mobile) return ['code'=>0,'msg'=>"联系电话不为空", 'data'=>''];
        if(!$email) return ['code'=>0,'msg'=>"联系邮箱不为空", 'data'=>''];
        $params['class'] = $class;
        $params['title'] = $title;
        $params['mobile'] = $mobile;
        $params['email'] = $email;

        $is_default = isset($data['is_default']) && $data['is_default'] ? $data['is_default'] : 0;
        $params['is_default'] = $is_default;

        try{
            if($is_default){
                \app\admin\model\Invoice::where(['user_id' => $user_id,'is_default'=>1])->update(['is_default'=>0]);
            }
            $res = \app\admin\model\Invoice::where(['user_id' => $user_id,'id'=>$id])->update($params);

            Db::commit();
            if($res){
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }

    }

    // 抬头列表
    public static function lst($data){
        $auth = Auth::instance();
        $user_id = $auth->id;
        $limit = isset($data['limit']) && $data['limit'] ? $data['limit'] : 10;

        $list = \app\admin\model\Invoice::where(['user_id'=>$user_id])->order('is_default desc,createtime desc')->field
        ('id,type,title,mobile,email,is_default,FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%i:%s") as createtime_text')->paginate($limit);
        return ['code'=>1,'msg'=>'查询成功','data'=>$list];
    }

    // 抬头列表
    public static function delete($data){
        $auth = Auth::instance();
        $user_id = $auth->id;
        $id = $data['id'];
        if(!$id) return ['code'=>0,'msg'=>"非法参数", 'data'=>''];

        $result = \app\admin\model\Invoice::where(['user_id'=>$user_id,'id'=>$id])->delete();
        if($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
        }
    }



}